import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtQuick.Controls.Styles 1.4
import QtGraphicalEffects 1.0
import Db 1.0
import User 1.0
//import Draw 1.0
Rectangle {
    id: root
    visible: true
    width: 1280
    height: 775
    color: "cadetblue"

    property string username: "客人"
    property string authority: "1"
    Dbutil{id:dboperate}
    //User{id:user}
    //Draw{id:draw}
    Image {
        id: rocket
        anchors.fill: parent
        fillMode: Image.TileHorizontally
        smooth: true
        source: "qrc:///images/background.jpg"
    }
    Popup //登陆弹窗
    {
        id: myPopup
        width: 400
        height: 600
        x: (root.width - width) / 2
        y: (root.height - height) / 2
        topPadding: 0
        leftPadding: 0
        rightPadding: 0
        bottomPadding: 0
        modal: false
        focus: true
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent

        Rectangle {
            width: parent.width
            height: parent.height
            Rectangle {
                width: parent.width
                height: 50
                color: "#002aff"
                Text {
                    x: 20
                    anchors.verticalCenter: parent.verticalCenter
                    text: "用户登陆/注销..."
                    color: "white"
                }
                Image {
                    id: close
                    y: 5
                    width: 40
                    height: 40
                    anchors.right: parent.right
                    source: "qrc:///images/close.png"
                    MouseArea {
                        anchors.fill: parent
                        acceptedButtons: Qt.LeftButton

                        onClicked: {
                            myPopup.close()
                            passwd.text=""
                        }
                    }
                }
            }
            Text {
                x: 40
                y: 65
                width: 60
                height: 30

                text: "用户名称："
            }
            ComboBox {
                id: combox
                x: 125
                y: 60
                width: 200
                height: 30
                currentIndex: 2
                model: ListModel {
                    id: cbItems
                    ListElement {
                        text: "用户管理员"
                    }
                    ListElement {
                        text: "系统管理员"
                    }
                    ListElement {
                        text: "客人"
                    }
                }

                onCurrentIndexChanged: {
                    username=cbItems.get(currentIndex).text
                }
            }
            Text {
                x: 40
                y: 105
                width: 60
                height: 30

                text: " 口 令："
            }
            Rectangle {
                x: 125
                y: 95
                width: 190
                height: 30
                border.color: "blue"
                TextInput {
                    id:passwd
                    width: parent.width
                    height: parent.height
                    echoMode: TextInput.Password
                    passwordCharacter: "*"
                }
            }
            Grid {
                id:num
                x: 115
                y: 120
                width: 200
                height:250
                padding: 10
                columns: 3
                rows: 4
                Button {
                    id: button1
                    width: 50
                    height:50
                    text: qsTr("1")
                    onClicked: passwd.insert(passwd.text.length,"1")
                }
                Button {
                    id: button2
                    width: 50
                    height:50
                    text: qsTr("2")
                    onClicked: passwd.insert(passwd.text.length,"2")
                }
                Button {
                    id: button3
                    width: 50
                    height:50
                    text: qsTr("3")
                    onClicked: passwd.insert(passwd.text.length,"3")
                }
                Button {
                    id: button4
                    width: 50
                    height:50
                    text: qsTr("4")
                    onClicked: passwd.insert(passwd.text.length,"4")
                }
                Button {
                    id: button5
                    width: 50
                    height:50
                    text: qsTr("5")
                    onClicked: passwd.insert(passwd.text.length,"5")
                }
                Button {
                    id: button6
                    width: 50
                    height:50
                    text: qsTr("6")
                    onClicked: passwd.insert(passwd.text.length,"6")
                }
                Button {
                    id: button7
                    width: 50
                    height:50
                    text: qsTr("7")
                    onClicked: passwd.insert(passwd.text.length,"7")
                }
                Button {
                    id: button8
                    width: 50
                    height:50
                    text: qsTr("8")
                    onClicked: passwd.insert(passwd.text.length,"8")
                }
                Button {
                    id: button9
                    width: 50
                    height:50
                    text: qsTr("9")
                    onClicked: passwd.insert(passwd.text.length,"9")
                }
                Button {
                    id: button0
                    width: 50
                    height:50
                    text: qsTr("0")
                    onClicked: passwd.insert(passwd.text.length,"0")
                }
                Button {
                    id: button10
                    width: 50
                    height:50
                    text: qsTr("<-")
                    onClicked: passwd.remove(passwd.text.length-1,passwd.text.length)
                }

            }
            Rectangle{
                width: parent.width
                height:60
                y:parent.height-60
                border.color: "blue"
                //property string authority: "1"
                Button{
                    x:250;y:10
                    width: 60
                    height: 40
                    text: "登陆"
                    onClicked: {
//                      myPopup.close()
                       // dboperate.connect()

                        if(username.match("用户管理员"))
                        {user2.findUserAuthority("lt",passwd.text)}
                        if(username.match("系统管理员"))
                        {user2.findUserAuthority("lmx",passwd.text)}
                        if(username.match("客人"))
                        {user2.findUserAuthority("bxr",passwd.text)}

                        myPopup.close()
                        //console.log(authority)
                        //console.log("hhh");
                        if(user2.getboolnum()){
                            login2.visible=false;
                            guestidentify.text=username;
                        }
                        passwd.text=""
                    }
                }
                Button{
                    x:320;y:10
                    width: 60
                    height: 40
                    text: "取消"
                    onClicked: {
                        myPopup.close()
                        passwd.text=""
                    }
                }
            }
        }
    }

    // Header{}
    Rectangle {
        id: time
        x: 1000+ 30
        y: 100
        width: 180
        height: 25
        color: "black"
        Timer {
            interval: 500; running: true; repeat: true
            onTriggered: timetext.text = Qt.formatDateTime(new Date(),
                                    "yyyy-MM-dd hh:mm:ss")
        }
        Text {
            id:timetext
            anchors.centerIn: parent
            color: "white"
            font.family: "Ubuntu"
            font.pixelSize: 15
        }
    }

    Rectangle {
        id: admin
        x: 180- 20
        y: 200
        width: 140
        height: 25
        color: "darkblue"
        Text {
            text: "用户管理"
            anchors.centerIn: parent
            color: "white"
            font.family: "Ubuntu"
            font.pixelSize: 20
        }
    }
    Rectangle {
        id: login2
        x: 180- 20
        y: admin.y + 75
        width: 140
        height: 25
        color: "darkblue"
        Text {
            text: "用户登录"
            anchors.centerIn: parent
            color: "white"
            font.family: "Ubuntu"
            font.pixelSize: 20
        }
        MouseArea {
            anchors.fill: parent
            acceptedButtons: Qt.LeftButton | Qt.RightButton

            onClicked: {
                myPopup.open()
            }
        }
    }
    Rectangle {
        id: logout
        x: 180 - 20
        y: admin.y + 150
        width: 140
        height: 25
        color: "darkblue"
        Text {
            text: "用户注销"
            anchors.centerIn: parent
            color: "white"
            font.family: "Ubuntu"
            font.pixelSize: 20
        }
        MouseArea{
            anchors.fill: parent
            onClicked: {
                user2.setauthority(1)
                login2.visible=true
                guestidentify.text="客人"
            }
        }
    }
    Text {
        id: guestText
        x: 180
        y: admin.y + 270
        text: "当前用户"
        color: "white"
        font.family: "Ubuntu"
        font.pixelSize: 20
    }
    Rectangle {
        id: guest
        x: 180 - 20
        y: admin.y + 300
        width: 140
        height: 25
        color: "darkblue"
        Text {
            id:guestidentify
            text: "客人"
            anchors.centerIn: parent
            color: "white"
            font.family: "Ubuntu"
            font.pixelSize: 20
        }
    }

    Text {
        id: systemName
        x: 400+ 100
        y: admin.y - 20
        text: "烟气数据监测系统"
        color: "white"
        font.family: "Ubuntu"
        font.pixelSize: 50
    }
}
